package com.red.star.wechat.work.site.banner;


import com.red.star.wechat.data.entity.BannerTemplate;
import com.red.star.wechat.data.entity.SysParam;
import com.red.star.wechat.data.mappers.BannerTemplateMapper;
import com.red.star.wechat.data.utils.CheckUtil;
import com.red.star.wechat.data.utils.FileUtil;
import com.red.star.wechat.work.constant.AdminSessionHolder;
import com.red.star.wechat.work.entity.Admin;
import com.red.star.wechat.work.entity.TableContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: banner模板管理
 * @Author: liucancan
 * @Date: 2018/9/26
 */
@RestController
@RequestMapping("/work")
public class BannerTeamplateController {

    @Autowired
    private BannerTemlateService bannerTemlateService;

    @Resource
    private BannerTemplateMapper bannerTemplateMapper;

    /**
     * @Description: banner模板管理页面
     * @Param: []
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: liucancan
     * @Date: 2018/9/26
     */
    @RequestMapping("/bannerTemplate")
    public ModelAndView bannerTemplate() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("bannertemplate");
        try {
            String token = FileUtil.fetchToken(SysParam.MACALLINE_FILE_EMPLOYEE_ID);
            if (CheckUtil.isEmpty(token)) {
                token = "";
            }
            modelAndView.addObject("token", token);
        } catch (Exception e) {
            System.out.println("获取token异常");
        }
        modelAndView.addObject("fileUploadUrl", SysParam.MACALLINE_FILE_UPLOAD_URL);
        return modelAndView;
    }


    /**
     * @Description: banner模板列表
     * @Param: [testData, firstTime, endTime]
     * @return: ResponseEntity<?>
     * @Author: liucancan
     * @Date: 2018/9/26
     */
    @RequestMapping(value = "listBannerTeamplate")
    public ResponseEntity listTestData(BannerTemplate bannerTeamplate) {
        TableContainer tableContainer = bannerTemlateService.listBannerTeamlate(bannerTeamplate);
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }

    /**
     * @Description: 新增
     * @Param: [bannerTeamplate]
     * @return: org.springframework.http.ResponseEntity<?>
     * @Author: liucancan
     * @Date: 2018/9/26
     */
    @RequestMapping(value = "addBannerTemplate")
    public ResponseEntity addBannerTemlate(BannerTemplate bannerTeamplate) {
        Admin admin = AdminSessionHolder.get();
        bannerTeamplate.setMallCode(admin.getMallCode());
        bannerTeamplate.setCreateTime(new Date());
        bannerTeamplate.setUpdateTime(bannerTeamplate.getCreateTime());
        int insert = bannerTemplateMapper.insert(bannerTeamplate);
        return new ResponseEntity<>(insert > 0 ? true : false, insert > 0 ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }
}
